﻿Public Class FrmVendor
    Dim Cboonload As Boolean = True
    Dim MyConn As New ClsSQLhelper
    Dim Strsql As String
    Dim Mysql1 As New ClsSQLhelper
    Dim On_load As Boolean = True
    Private Sub frmVendor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Settings.CurrentuserGroup <> 1 Then Me.CboSite.Enabled = False
        Load_ComboboxData_site()
        Load_Data(Me.CboSite.SelectedValue)
    End Sub
    Function Validate_VendorDetail() As Boolean
        Try
            Validate_VendorDetail = False
            If Me.txtCompanyname.Text.Trim = "" Then
                MsgBox("กรุณากรอกชื่อบริษัท", MsgBoxStyle.Exclamation, "ข้อมูลไม่ครบถ้วน")
                Exit Function
            End If


            Validate_VendorDetail = True
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function
    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            If MsgBox("คุณต้องการบันทึกข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub


            Dim DS As New DataSet
            If Me.Txtid.Text = "" Then
                If Validate_VendorDetail() = False Then Exit Sub
                Strsql = "Select * from vendor where vendorname= '" & Me.txtCompanyname.Text & "'"
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_vendor", "vendor")
                If DS.Tables(0).Rows.Count <> 0 Then
                    MsgBox("รายการ นี้มีอยู่แล้วไม่สามารถบันทึกซ้ำได้อีก", MsgBoxStyle.Critical)
                    Exit Sub
                End If
                Strsql = "Insert into vendor (vendorname,address1,tmppart,amppart,chwpart,zipcode,tel,tel2,fax,fax2,mobile,email,website,note,active,siteid,createdby,updatedby,taxid,contactname) "
                Strsql = Strsql & " values ('" & Me.txtCompanyname.Text & "', '" & Me.TxtAdd1.Text & "'," & Me.cboTmb.SelectedValue & "," & Me.cboAMPHUR.SelectedValue & " , " & Me.CboProvince.SelectedValue
                Strsql = Strsql & ", '" & Me.TxtZipcode.Text & "', '" & Me.TxtTel1.Text & "','" & Me.TxtTel2.Text & "','" & Me.TxtFax1.Text & "','" & Me.TxtFax2.Text & "','" & Me.txtmobile.Text & "'"
                Strsql = Strsql & ", '" & Me.TxtEmail.Text & "','" & Me.TxtWebsite.Text & "'"
                Strsql = Strsql & ",'" & Me.TxtNote.Text & "'," & Me.ChkActive.Checked & "," & Me.CboSite.SelectedValue & " ,'" & My.Settings.Currentuser & "','" & My.Settings.Currentuser & "','" & Me.TxtTaxID.Text & "','" & Me.TxtContactname.Text & "')"
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)
                MsgBox("เพิ่มข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            Else

                Strsql = "update vendor set contactname = '" & Me.TxtContactname.Text & "',vendorname= '" & Me.txtCompanyname.Text & "',taxid = '" & Me.TxtTaxID.Text & "', address1 = '" & Me.TxtAdd1.Text & "',tmppart = " & Me.cboTmb.SelectedValue & ",amppart = " & Me.cboAMPHUR.SelectedValue
                Strsql = Strsql & " ,chwpart = " & Me.CboProvince.SelectedValue & ",zipcode = '" & Me.TxtZipcode.Text & "',tel = '" & Me.TxtTel1.Text & "',tel2 = '" & Me.TxtTel2.Text & "',fax = '" & Me.TxtFax1.Text & "',fax2 = '" & Me.TxtFax2.Text & "', mobile='" & Me.txtmobile.Text & "'"
                Strsql = Strsql & " , note = '" & Me.TxtNote.Text & "' ,active = " & Me.ChkActive.Checked
                Strsql = Strsql & " , website = '" & Me.TxtWebsite.Text & "',email = '" & Me.TxtEmail.Text & "',updatedby = '" & My.Settings.Currentuser & "'"
                Strsql = Strsql & " where vendorid = " & Me.Txtid.Text.Trim
                Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                Mysql1.MySQLExecute(Strsql)
                MsgBox("ปรับปรุงข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)

            End If
            'Refresh Data
            Load_Data(Me.CboSite.SelectedValue)
            New_Entry()
            Me.TabVendor.TabPages(0).Select()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Function Load_ComboboxData_site() As Boolean
        Try
            Dim DS As New DataSet

            Load_ComboboxData_site = False

            Strsql = "select * from site order by site"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_site", "site")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboSite
                If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "site"
                .ValueMember = "siteid"
                .SelectionStart = 0
                If My.Settings.CurrentSite <> "" Then
                    .SelectedValue = My.Settings.CurrentSite
                End If
            End With
            Load_ComboboxData_site = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_site - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)

        End Try
    End Function
    Sub New_Entry()
        Me.Txtid.Text = ""
        Me.txtCompanyname.Text = ""
        Me.TxtAdd1.Text = ""
        Me.cboTmb.Text = ""
        Me.cboAMPHUR.Text = ""
        Me.CboProvince.Text = ""
        Me.TxtZipcode.Text = ""
        Me.TxtTel1.Text = ""
        Me.TxtTel2.Text = ""
        Me.TxtFax1.Text = ""
        Me.TxtFax2.Text = ""
        Me.TxtWebsite.Text = ""
        Me.TxtEmail.Text = ""
        Me.TxtNote.Text = ""
        Me.TxtTaxID.Text = ""
        Me.TxtCreatedby.Text = My.Settings.Currentuser
        Me.TxtUpdatedBy.Text = My.Settings.Currentuser
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            If MsgBox("คุณต้องการลบข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub

            Dim Strsql As String
            Dim Mysql1 As New ClsSQLhelper
            Strsql = "Delete from vendor where vendorid = " & Me.Txtid.Text
            Debug.Print(Strsql)
            Mysql1.MySQLExecute(Strsql)
            'Refresh Data
            Load_Data(Me.CboSite.SelectedValue)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Sub Load_Data(ByVal siteid As Integer, Optional ByVal Str_Search As String = "")
        Try

            Dim DS, DS2, DS3, DS4, DS5, DS6, DS7, DS8, DS9 As New DataSet

            Strsql = "select vendorid,vendorname,address1,district_name,tblamphur.AMPHUR_NAME,tblprovince.PROVINCE_NAME,zipcode,vendor.tel,vendor.tel2,vendor.fax,vendor.fax2,vendor.mobile,vendor.note from vendor"
            Strsql = Strsql & " left join tbldistrict on vendor.tmppart =  tbldistrict.DISTRICT_ID  "
            Strsql = Strsql & " left join tblamphur on vendor.amppart= tblamphur.AMPHUR_ID "
            Strsql = Strsql & " left join tblprovince on vendor.chwpart= tblprovince.PROVINCE_ID  where siteid = " & siteid
            If Str_Search <> "" Then Strsql = Strsql & " and  vendorname like '" & Str_Search & "%' "
            Strsql = Strsql & " order by active desc,vendorname"
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_Vendor", "vendor")

            Pn_Framework.Set_GridSetting(Me.griddetail, DS)


            DS4 = MyConn.GetMYSQLDataset("Select PROVINCE_ID,PROVINCE_NAME from tblprovince", "Dataset3", "tblprovince")
            With Me.CboProvince
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DS4.Tables(0)
                .DisplayMember = "PROVINCE_NAME"
                .ValueMember = "PROVINCE_ID"
            End With
            DS5 = MyConn.GetMYSQLDataset("Select AMPHUR_ID,AMPHUR_NAME from tblAMPHUR where PROVINCE_ID = " & Me.CboProvince.SelectedValue.ToString, "Dataset5", "tblAMPHUR")
            'DS5 = MyConn.GetDataset("Select AMPHUR_ID,AMPHUR_NAME from tblAMPHUR")
            With Me.cboAMPHUR
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DS5.Tables(0)
                .DisplayMember = "AMPHUR_NAME"
                .ValueMember = "AMPHUR_ID"
            End With
            DS6 = MyConn.GetMYSQLDataset("Select DISTRICT_ID,DISTRICT_NAME from tbldistrict  where PROVINCE_ID = " & Me.CboProvince.SelectedValue.ToString & " and AMPHUR_ID = " & Me.cboAMPHUR.SelectedValue.ToString, "Dataset6", "tbldistrict")
            'DS6 = MyConn.GetDataset("Select DISTRICT_ID,DISTRICT_NAME from tbldistrict")
            With Me.cboTmb
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = DS6.Tables(0)
                .DisplayMember = "DISTRICT_NAME"
                .ValueMember = "DISTRICT_ID"
            End With
            Cboonload = False
            On_load = False

        Catch ex As Exception

        End Try
    End Sub

    Private Sub griddetail_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles griddetail.CellClick
        With Me.griddetail
            If .RowCount = 0 Then Exit Sub
            Strsql = "select * from vendor where vendorid = " & .Item(0, e.RowIndex).Value.ToString
            Dim DS_Vendor As New DataSet
            DS_Vendor = Mysql1.GetMYSQLDataset(Strsql, "DSVendor", "Vendor")
            Me.Txtid.Text = DS_Vendor.Tables(0).Rows(0).Item("vendorid").ToString
            Me.TxtContactname.Text = DS_Vendor.Tables(0).Rows(0).Item("contactname").ToString
            Me.TxtTaxID.Text = DS_Vendor.Tables(0).Rows(0).Item("taxid").ToString
            Me.txtCompanyname.Text = DS_Vendor.Tables(0).Rows(0).Item("vendorname").ToString
            Me.TxtAdd1.Text = DS_Vendor.Tables(0).Rows(0).Item("address1").ToString
            Me.cboTmb.SelectedValue = DS_Vendor.Tables(0).Rows(0).Item("tmppart")
            Me.cboAMPHUR.SelectedValue = DS_Vendor.Tables(0).Rows(0).Item("amppart")
            Me.CboProvince.SelectedValue = DS_Vendor.Tables(0).Rows(0).Item("chwpart")
            Me.TxtZipcode.Text = DS_Vendor.Tables(0).Rows(0).Item("zipcode").ToString
            Me.TxtTel1.Text = DS_Vendor.Tables(0).Rows(0).Item("tel").ToString
            Me.TxtTel2.Text = DS_Vendor.Tables(0).Rows(0).Item("tel2").ToString
            Me.TxtFax1.Text = DS_Vendor.Tables(0).Rows(0).Item("fax").ToString
            Me.TxtFax2.Text = DS_Vendor.Tables(0).Rows(0).Item("fax2").ToString
            Me.TxtWebsite.Text = DS_Vendor.Tables(0).Rows(0).Item("website").ToString
            Me.TxtEmail.Text = DS_Vendor.Tables(0).Rows(0).Item("email").ToString
            Me.TxtNote.Text = DS_Vendor.Tables(0).Rows(0).Item("note").ToString
            Me.TxtCreatedby.Text = DS_Vendor.Tables(0).Rows(0).Item("createdby").ToString
            Me.TxtUpdatedBy.Text = DS_Vendor.Tables(0).Rows(0).Item("updatedby").ToString
            Me.ChkActive.Checked = DS_Vendor.Tables(0).Rows(0).Item("siteid")


        End With
        With Me.TabVendor
            .SelectedTab = TabDetail
        End With
        Me.BtnSave.Enabled = True
        Me.BtnDelete.Enabled = True
    End Sub
    Private Sub BtnCancle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancle.Click
        Me.Dispose()
    End Sub

    Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
        New_Entry()
        Me.TabVendor.SelectedTab = TabDetail
        Me.BtnSave.Enabled = True

    End Sub

    Private Sub TabDetail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabDetail.Click

    End Sub

    Private Sub CboProvince_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboProvince.SelectedIndexChanged
        Try
            If Cboonload = True Then Exit Sub
            Dim ds7, ds8 As New DataSet
            ds7 = MyConn.GetMYSQLDataset("Select AMPHUR_ID,AMPHUR_NAME from tblAMPHUR where PROVINCE_ID = " & Me.CboProvince.SelectedValue.ToString, "Dataset7", "tblAMPHUR")
            With Me.cboAMPHUR
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = ds7.Tables(0)
                .DisplayMember = "AMPHUR_NAME"
                .ValueMember = "AMPHUR_ID"
            End With
            ds8 = MyConn.GetMYSQLDataset("Select DISTRICT_ID,DISTRICT_NAME from tbldistrict  where PROVINCE_ID = " & Me.CboProvince.SelectedValue.ToString & " and AMPHUR_ID = " & Me.cboAMPHUR.SelectedValue.ToString, "Dataset8", "tbldistrict")
            With Me.cboTmb
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = ds8.Tables(0)
                .DisplayMember = "DISTRICT_NAME"
                .ValueMember = "DISTRICT_ID"
            End With
        Catch ex As Exception
            'MsgBox(Err.Number & " : " & ex.Message)
        End Try
    End Sub

    Private Sub cboDISTRICT_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTmb.SelectedIndexChanged

    End Sub

    Private Sub cboAMPHUR_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAMPHUR.SelectedIndexChanged
        Try
            If Cboonload = True Then Exit Sub
            Dim ds9 As New DataSet
            ds9 = MyConn.GetMYSQLDataset("Select DISTRICT_ID,DISTRICT_NAME from tbldistrict  where PROVINCE_ID = " & Me.CboProvince.SelectedValue.ToString & " and AMPHUR_ID = " & Me.cboAMPHUR.SelectedValue.ToString, "Dataset6", "tbldistrict")
            With Me.cboTmb
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataSource = ds9.Tables(0)
                .DisplayMember = "DISTRICT_NAME"
                .ValueMember = "DISTRICT_ID"
            End With
        Catch ex As Exception

        End Try
    End Sub

    Private Sub CboSite_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboSite.SelectedIndexChanged
        If On_Load = False Then
            Load_Data(Me.CboSite.SelectedValue)
            Me.TabVendor.SelectedTab = TabNameList

        End If
    End Sub

    Private Sub griddetail_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles griddetail.CellContentClick

    End Sub

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        Load_Data(Me.CboSite.SelectedValue, Me.TxtSearch.Text)
    End Sub
End Class